﻿using Hotel.Model.Discount;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.DAL
{
    public class DiscountDAL
    {
        MyDBContext db;
        public DiscountDAL(MyDBContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 增加优惠券
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public int  PostAddDiscount( T_Discount info)
        {
            try
            {
                db.T_Discount.Add(info);
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 查询优惠券的标题是否重复
        /// </summary>
        /// <param name="title"></param>
        /// <returns></returns>
        public int SeachDiscountTile( string title)

        {
            try
            {
                int res  = db.T_Discount.Where(c => c.D_Title == title).Count();
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取优惠券类型列表
        /// </summary>
        /// <returns>返回符合条件的数据</returns>
        public List<T_Discount_Type> GetDiscountTypeList()
        {
            try
            {
                List<T_Discount_Type> list = db.T_Discount_Type.ToList();
               return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
        
        /// <summary>
        /// 获取所有优惠券
        /// </summary>
        /// <param name="totalPage"></param>
        /// <param name="totalCount"></param>
        /// <param name="Ddid"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public  IQueryable GetDiscountList(out int totalPage,out int totalCount,int ?DTid=null,int pageIndex=1,int pageSize=2) {
            try
            {
                //var list = db.T_Discount.Join(db.T_Discount_Type, a => a.DT_Id, b => b.DT_Id, (a, b) => new
                //{
                //    a.D_Id,
                //    a.StoreId,
                //    a.D_Title,
                //    b.DT_Name,
                //    b.DT_Id
                //}).Join(db.T_Store, a => a.StoreId, b => b.StoreId, (a, b) => new
                //{
                //    a.D_Id,
                //    a.StoreId,
                //    a.D_Title,
                //    a.DT_Name,
                //    a.DT_Id,
                //    b.StoreName
                //});


                var list = db.T_Discount.Join(db.T_Discount_Type, a => a.DT_Id, b => b.DT_Id, (a, b) => new
                {
                    a.D_Id,
                    a.StoreId,
                    a.D_Title,
                    b.DT_Name,
                    b.DT_Id
                });
                ///优惠券类型查询
                if (DTid != null)
                {
                    list = list.Where(c => c.DT_Id == DTid);
                }

                //分页

                totalCount = list.Count();
                totalPage = (int)Math.Ceiling(totalCount * 1.0 / pageSize);

                list = list.OrderBy(c => c.D_Id).Skip((pageIndex - 1) * pageSize).Take(pageSize);

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 查询所有使用过的优惠券
        /// </summary>
        /// <returns></returns>
        public IQueryable GetDiscountUsed()
        {
            try
            {
               var list =  db.T_Discount_Member.Join(db.T_Member, a => a.M_Id, b => b.M_Id, (a, b) => new
                {
                    a.D_Id,
                    a.Use_Time,
                    a.Use_State,
                    a.Send_Time,
                    b.M_Phone,
                    b.M_Name,

               }).Join(db.T_Discount, a => a.D_Id, b => b.D_Id, (a,b) => new
               {
                   a.D_Id,
                   a.Use_Time,
                   a.Use_State,
                   a.Send_Time,
                   a.M_Phone,
                   a.M_Name,
                   b.D_Title,
                   b.D_EenTime,
                   b.D_BeginTime
               })
                
                .Where(c => c.Use_State == true);

                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
